﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Donal.Xms.DBUtility;
using Donal.Xms.Common;
using Donal.Xms.Model;//请先添加引用 

namespace Donal.Xms.DAL
{
    public class article_like
    {
        #region 查询是否存在该记录(根据关键字或者条件语句,但是条件语句优先)
        /// <summary>
        /// 查询是否存在该记录
        /// </summary>
        /// <param name="id">关键字</param>
        /// <param name="strWhere">条件语句</param>
        /// <returns></returns>
        public bool Exists(int id, string strWhere = "")
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"Select Count(*) from [dt_article_like]");
            if (strWhere.Trim() != "")
            {
                strSql.Append(@" where " + strWhere);
                return DbHelperSQL.Exists(strSql.ToString());
            }
            else
            {
                strSql.Append(@" where [id]=@id");
                SqlParameter[] parameters = new SqlParameter[]{ 
                                             new SqlParameter("@id",SqlDbType.Int,4)
                                          };
                parameters[0].Value = id;
                return DbHelperSQL.Exists(strSql.ToString(), parameters);
            }
        }
        #endregion

        #region 删除一条记录
        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <param name="id">关键字</param>
        /// <returns>删除结果</returns>
        public int Del(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"delete [dt_article_like] where [id]='" + id + "'");
            return DbHelperSQL.ExecuteSql(strSql.ToString());
        }
        #endregion

        #region 删除一条记录
        /// <summary>
        /// 删除一条记录
        /// </summary>
        public bool Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from  [dt_article_like]");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
            		new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0) { return true; }
            else { return false; }
        }
        #endregion

        #region 得到一个对象实体
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Donal.Xms.Model.article_like GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 * from [dt_article_like]");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
            		new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0) { return DataRowToModel(ds.Tables[0].Rows[0]); }
            else { return null; }
        }
        #endregion

        #region 将对象转换为实体
        /// <summary>
        /// 将对象转换为实体
        /// </summary>
        public Donal.Xms.Model.article_like DataRowToModel(DataRow dr)
        {
            Donal.Xms.Model.article_like model = new Donal.Xms.Model.article_like();
            if (dr != null)
            {
                if (dr["id"] != null && dr["id"].ToString() != "")
                {
                    model.Id = int.Parse(dr["id"].ToString());
                }
                if (dr["ip"] != null && dr["ip"].ToString() != "")
                {
                    model.Ip = dr["ip"].ToString();
                }
                if (dr["userAgent"] != null && dr["userAgent"].ToString() != "")
                {
                    model.UserAgent = dr["userAgent"].ToString();
                }
                if (dr["article_id"] != null && dr["article_id"].ToString() != "")
                {
                    model.Article_id = int.Parse(dr["article_id"].ToString());
                }
            }
            return model;
        }
        #endregion

        #region 获得前几行数据
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder(); strSql.Append("select ");
            if (Top > 0) { strSql.Append(" top " + Top.ToString()); }
            strSql.Append(" * from [dt_article_like] ");
            if (strWhere.Trim() != "") { strSql.Append(" where " + strWhere); }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }
        #endregion

        #region 获得记录数
        /// <summary>
        /// 获得记录数
        /// </summary>
        public int GetCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder(); strSql.Append("select count(1) ");

            strSql.Append(" from [dt_article_like] ");
            if (strWhere.Trim() != "") { strSql.Append(" where " + strWhere); }

            return Convert.ToInt32(DbHelperSQL.GetSingle(strSql.ToString()));
        }
        #endregion


        #region 获得单个实体对象
        /// <summary>
        /// 获得单个实体对象
        /// </summary>
        /// <param name="id">关键字</param>
        /// <returns>实体对象</returns>
        public Donal.Xms.Model.article_like SelectModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"select [id],[ip],[userAgent],[article_id] from [dt_article_like] ");
            strSql.Append(@" where [id]=@id ");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;
            Donal.Xms.Model.article_like model = new Donal.Xms.Model.article_like();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                LoadEntityData(ref model, ds.Tables[0].Rows[0]);
                return model;
            }
            else
            {
                return null;
            }
        }
        #endregion

        #region 根据条件查询实体记录
        /// <summary>
        /// 根据条件查询实体记录
        /// </summary>
        /// <param name="whereStr">查询条件</param>
        /// <returns>实体记录</returns>
        public IList<Donal.Xms.Model.article_like> SelectList(string whereStr)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"select [id],[ip],[userAgent],[article_id] from [dt_article_like] ");
            if (whereStr.Trim() != "")
            {
                strSql.Append(@" where " + whereStr);
            }
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            List<Donal.Xms.Model.article_like> list = null;
            if (ds.Tables[0].Rows.Count > 0)
            {
                list = new List<Donal.Xms.Model.article_like>();
                Donal.Xms.Model.article_like model = null;

                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    model = new Donal.Xms.Model.article_like();
                    LoadEntityData(ref model, dr);
                    list.Add(model);
                }
            }
            return list;
        }

        #endregion

        #region 分页查询
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pageSize">页面显示的行数</param>
        /// <param name="pageIndex">页号</param>
        /// <param name="pageCount">总行数</param>
        /// <returns>根据分页查询的数据</returns>
        public IList<Donal.Xms.Model.article_like> SelectByPaged(int pageSize, int pageIndex, out int pageCount)
        {
            string sql_count = "select COUNT(*) from [dt_article_like]";
            string sql_select = @"select * from 
	                                (select ROW_NUMBER() over (order by [id]) 'RowIndex',* from [dt_article_like]) as Temp
	                                where RowIndex >" + Convert.ToString(pageSize * pageIndex) + @"
	                                and RowIndex <=" + Convert.ToString(pageSize * (pageIndex + 1));
            pageCount = int.Parse(DbHelperSQL.GetSingle(sql_count).ToString());
            DataTable dt = new DataTable();
            DataSet ds = DbHelperSQL.Query(sql_select);
            if (ds != null && ds.Tables.Count > 0) { dt = ds.Tables[0]; } else { dt = null; }
            IList<Donal.Xms.Model.article_like> list = new List<Donal.Xms.Model.article_like>();
            LoadListData(ref list, dt);
            return list;
        }
        #endregion

        #region 分页查询
        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM [dt_article_like] ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }
        #endregion

        #region 往实体集合中添加记录
        /// <summary>
        /// 往实体集合中添加记录
        /// </summary>
        /// <param name="list">实体列表</param>
        /// <param name="dt">表</param>
        private void LoadListData(ref IList<Donal.Xms.Model.article_like> list, DataTable dt)
        {
            if (dt.Rows.Count > 0)
            {
                Donal.Xms.Model.article_like model;
                foreach (DataRow dr in dt.Rows)
                {
                    model = new Donal.Xms.Model.article_like();
                    LoadEntityData(ref model, dr);
                    list.Add(model);
                }
            }
        }
        #endregion

        #region 往实体中存放数据
        /// <summary>
        /// 往实体中存放数据
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <param name="dr">行记录</param>
        private void LoadEntityData(ref Donal.Xms.Model.article_like model, DataRow dr)
        {
            if (dr["id"].ToString() != "") model.Id = int.Parse(dr["id"].ToString());
            if (dr["ip"].ToString() != "") model.Ip = dr["ip"].ToString();
            if (dr["userAgent"].ToString() != "") model.UserAgent = dr["userAgent"].ToString();
            if (dr["article_id"].ToString() != "") model.Article_id = int.Parse(dr["article_id"].ToString());
        }
        #endregion

        #region 新增
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model">要往数据库中的添加的实体对象</param>
        /// <returns>结果</returns>
        public int Add(Donal.Xms.Model.article_like model)
        {
            int result = 0;
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append(@"insert into [dt_article_like]( ");
                strSql.Append(@"[ip],[userAgent],[article_id]) values (@ip,@userAgent,@article_id)");
                SqlParameter[] parameters = new SqlParameter[] {
                    new SqlParameter("@ip", SqlDbType.VarChar,50),
                    new SqlParameter("@userAgent", SqlDbType.VarChar,500),
                    new SqlParameter("@article_id", SqlDbType.Int,4)
                    };
                if (model.Ip != null) parameters[0].Value = model.Ip;
                else parameters[0].Value = System.DBNull.Value;
                if (model.UserAgent != null) parameters[1].Value = model.UserAgent;
                else parameters[1].Value = System.DBNull.Value;
                if (model.Article_id != null) parameters[2].Value = model.Article_id;
                else parameters[2].Value = System.DBNull.Value;
                int obj = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
                return obj;
            }
            catch (Exception)
            {
            }
            return result;
        }
        #endregion

        #region 更新
        /// <summary>
        /// 更新一条记录
        /// </summary>
        /// <param name="model">需要跟新的数据</param>
        /// <returns>更新结果</returns>
        public int Update(Donal.Xms.Model.article_like model)
        {
            int res = -2;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [dt_article_like] set ");
            strSql.Append("[ip] = @ip , ");
            strSql.Append("[userAgent] = @userAgent , ");
            strSql.Append("[article_id] = @article_id ");
            strSql.Append("  where [id]=@id");
            SqlParameter[] parameters = new SqlParameter[]  {
                    new SqlParameter("@ip", SqlDbType.VarChar,50),
                    new SqlParameter("@userAgent", SqlDbType.VarChar,500),
                    new SqlParameter("@article_id", SqlDbType.Int,4),
                    new SqlParameter("@id",SqlDbType.Int,4)
                                       };
            if (model.Ip != null) parameters[0].Value = model.Ip;
            else parameters[0].Value = System.DBNull.Value;
            if (model.UserAgent != null) parameters[1].Value = model.UserAgent;
            else parameters[1].Value = System.DBNull.Value;
            if (model.Article_id != null) parameters[2].Value = model.Article_id;
            else parameters[2].Value = System.DBNull.Value;
            parameters[3].Value = model.Id;
            try
            {
                res = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            }
            catch (Exception)
            {
            }
            return res;
        }
        #endregion
    }
}

